package com.salesys.gr.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.tony.domain.Conver;
import com.salesys.tony.service.ConverService;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Text;

public class FrmModifyConver {

	protected Shell FrmModifyConver;
	private Text txtRecommend;
	private Text txtChild_1;
	private Text txtChild_2;
	private Text txtChild_3;
	private Text txtChild_4;
	private Text txtChild_5;
	private Text txtChild_6;
	private Text txtChild_7;
	private Text txtChild_8;
	private Text txtChild_9;
	private Text txtChild_10;

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmModifyConver.open();
		FrmModifyConver.layout();
		while (!FrmModifyConver.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmModifyConver = new Shell(SWT.CLOSE | SWT.APPLICATION_MODAL);
		FrmModifyConver.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.keyCode == 27) {
					FrmModifyConver.close();
				}
			}
		});
		FrmModifyConver.setSize(270, 373);
		FrmModifyConver.setText("修改现金比率");
		// 窗口居中
		FrmModifyConver.setLocation(new FrmHelper().setLocationCenter(FrmModifyConver));

		Label lblMarkSource = new Label(FrmModifyConver, SWT.NONE);
		lblMarkSource.setBounds(55, 10, 50, 17);
		lblMarkSource.setText("现金来源");

		Label lblMarkPercent = new Label(FrmModifyConver, SWT.NONE);
		lblMarkPercent.setBounds(160, 10, 50, 17);
		lblMarkPercent.setText("产生比率");

		Label lblRecommend = new Label(FrmModifyConver, SWT.NONE);
		lblRecommend.setBounds(55, 40, 50, 17);
		lblRecommend.setText("推      荐");

		Label lblChild_1 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_1.setBounds(55, 63, 61, 17);
		lblChild_1.setText("一级加盟");

		Label lblChild_2 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_2.setBounds(55, 86, 50, 17);
		lblChild_2.setText("二级加盟");

		Label lblChild_3 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_3.setBounds(55, 109, 50, 17);
		lblChild_3.setText("三级加盟");

		Label lblChild_4 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_4.setBounds(55, 132, 50, 17);
		lblChild_4.setText("四级加盟");

		Label lblChild_5 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_5.setBounds(55, 155, 50, 17);
		lblChild_5.setText("五级加盟");

		Label lblChild_6 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_6.setBounds(55, 178, 50, 17);
		lblChild_6.setText("六级加盟");

		Label lblChild_7 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_7.setBounds(55, 201, 50, 17);
		lblChild_7.setText("七级加盟");

		Label lblChild_8 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_8.setBounds(55, 224, 50, 17);
		lblChild_8.setText("八级加盟");

		Label lblChild_9 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_9.setBounds(55, 247, 50, 17);
		lblChild_9.setText("九级加盟");

		Label lblChild_10 = new Label(FrmModifyConver, SWT.NONE);
		lblChild_10.setBounds(55, 270, 50, 17);
		lblChild_10.setText("十级加盟");

		txtRecommend = new Text(FrmModifyConver, SWT.BORDER);
		txtRecommend.setBounds(156, 40, 60, 20);
		try {
			txtRecommend.setText(new ConverService().queryMarkPercentFromMarkSource("recommend"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_1 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_1.setBounds(156, 63, 60, 20);
		try {
			txtChild_1.setText(new ConverService().queryMarkPercentFromMarkSource("child_1"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_2 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_2.setBounds(156, 86, 60, 20);
		try {
			txtChild_2.setText(new ConverService().queryMarkPercentFromMarkSource("child_2"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_3 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_3.setBounds(156, 109, 60, 20);
		try {
			txtChild_3.setText(new ConverService().queryMarkPercentFromMarkSource("child_3"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_4 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_4.setBounds(156, 132, 60, 20);
		try {
			txtChild_4.setText(new ConverService().queryMarkPercentFromMarkSource("child_4"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_5 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_5.setBounds(156, 155, 60, 20);
		try {
			txtChild_5.setText(new ConverService().queryMarkPercentFromMarkSource("child_5"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_6 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_6.setBounds(156, 178, 60, 20);
		try {
			txtChild_6.setText(new ConverService().queryMarkPercentFromMarkSource("child_6"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_7 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_7.setBounds(156, 201, 60, 20);
		try {
			txtChild_7.setText(new ConverService().queryMarkPercentFromMarkSource("child_7"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_8 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_8.setBounds(156, 224, 60, 20);
		try {
			txtChild_8.setText(new ConverService().queryMarkPercentFromMarkSource("child_8"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_9 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_9.setBounds(156, 247, 60, 20);
		try {
			txtChild_9.setText(new ConverService().queryMarkPercentFromMarkSource("child_9"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		txtChild_10 = new Text(FrmModifyConver, SWT.BORDER);
		txtChild_10.setBounds(156, 270, 60, 20);
		try {
			txtChild_10.setText(new ConverService().queryMarkPercentFromMarkSource("child_10"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		Button btnModify = new Button(FrmModifyConver, SWT.NONE);
		btnModify.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					modifyConver();
				}
			}
		});
		btnModify.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				modifyConver();
			}
		});
		btnModify.setBounds(166, 308, 88, 30);
		btnModify.setText("修　改");

		Label lblTip_0 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_0.setBounds(220, 40, 15, 17);
		lblTip_0.setText("%");

		Label lblTip_1 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_1.setText("%");
		lblTip_1.setBounds(220, 63, 15, 17);

		Label lblTip_2 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_2.setText("%");
		lblTip_2.setBounds(220, 86, 15, 17);

		Label lblTip_3 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_3.setText("%");
		lblTip_3.setBounds(220, 109, 15, 17);

		Label lblTip_4 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_4.setText("%");
		lblTip_4.setBounds(220, 132, 15, 17);

		Label lblTip_5 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_5.setText("%");
		lblTip_5.setBounds(220, 155, 15, 17);

		Label lblTip_6 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_6.setText("%");
		lblTip_6.setBounds(220, 178, 15, 17);

		Label lblTip_7 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_7.setText("%");
		lblTip_7.setBounds(220, 201, 15, 17);

		Label lblTip_8 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_8.setText("%");
		lblTip_8.setBounds(220, 224, 15, 17);

		Label lblTip_9 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_9.setText("%");
		lblTip_9.setBounds(220, 247, 15, 17);

		Label lblTip_10 = new Label(FrmModifyConver, SWT.NONE);
		lblTip_10.setText("%");
		lblTip_10.setBounds(220, 270, 15, 17);
	}

	public void modifyConver() {
		if (txtRecommend.getText() == "" || txtChild_1.getText() == "" || txtChild_2.getText() == "" || txtChild_3.getText() == "" || txtChild_4.getText() == "" || txtChild_5.getText() == "" || txtChild_6.getText() == "" || txtChild_7.getText() == "" || txtChild_8.getText() == "" || txtChild_9.getText() == "" || txtChild_10.getText() == "") {
			new MsgBox().errorTip(FrmModifyConver, "积分比率不能为空");
		} else {
			Conver con_0 = new Conver(1, "recommend", txtRecommend.getText());
			Conver con_1 = new Conver(2, "child_1", txtChild_1.getText());
			Conver con_2 = new Conver(3, "child_2", txtChild_2.getText());
			Conver con_3 = new Conver(4, "child_3", txtChild_3.getText());
			Conver con_4 = new Conver(5, "child_4", txtChild_4.getText());
			Conver con_5 = new Conver(6, "child_5", txtChild_5.getText());
			Conver con_6 = new Conver(7, "child_6", txtChild_6.getText());
			Conver con_7 = new Conver(8, "child_7", txtChild_7.getText());
			Conver con_8 = new Conver(9, "child_8", txtChild_8.getText());
			Conver con_9 = new Conver(10, "child_9", txtChild_9.getText());
			Conver con_10 = new Conver(11, "child_10", txtChild_10.getText());
			int affect_0 = 0, affect_1 = 0, affect_2 = 0, affect_3 = 0, affect_4 = 0, affect_5 = 0, affect_6 = 0, affect_7 = 0, affect_8 = 0, affect_9 = 0, affect_10 = 0;
			try {
				affect_0 = new ConverService().modifyMarkPercent(con_0);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_1 = new ConverService().modifyMarkPercent(con_1);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_2 = new ConverService().modifyMarkPercent(con_2);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_3 = new ConverService().modifyMarkPercent(con_3);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_4 = new ConverService().modifyMarkPercent(con_4);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_5 = new ConverService().modifyMarkPercent(con_5);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_6 = new ConverService().modifyMarkPercent(con_6);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_7 = new ConverService().modifyMarkPercent(con_7);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_8 = new ConverService().modifyMarkPercent(con_8);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_9 = new ConverService().modifyMarkPercent(con_9);
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				affect_10 = new ConverService().modifyMarkPercent(con_10);
			} catch (Exception e) {
				e.printStackTrace();
			}

			if (affect_0 == 1 && affect_1 == 1 && affect_2 == 1 && affect_3 == 1 && affect_4 == 1 && affect_5 == 1 && affect_6 == 1 && affect_7 == 1 && affect_8 == 1 && affect_9 == 1 && affect_10 == 1) {
				new MsgBox().infoTip(FrmModifyConver, "修改成功！");
				FrmModifyConver.close();
			} else {
				new MsgBox().errorTip(FrmModifyConver, "修改失败！");
			}
		}
	}
}